import { message } from "antd"
import axios from "axios"

const axiosInstance = axios.create({
    baseURL: "http://localhost:8118",
    timeout: 10 * 1000
})

export default axiosInstance

export type ResDataType = {
    // 对象类型
    [key: string]: any
}

// 根据api设计设置类型

export type ResType = {
    errno: number
    data?: ResDataType
    msg?: string
}

// 相应拦截器
axiosInstance.interceptors.response.use(res => {
    // 响应拦截器成功的回调
    // API设计中可以有些api不包括data

    const resData = (res.data || {}) as ResType

    const { errno, data, msg } = resData

    if (errno !== 0) {
        // 如果errno不等于，就进行错误提示
        // msg可能为空
        if (msg) {
            message.error(msg)
        }
        throw new Error(msg)
    }

    return data as any
})
